<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增出库单')"/>
    <th:block th:include="include :: datetimepicker-css"/>
    <style>
        table label.error {
            position: inherit;
        }

        select + label.error {
            z-index: 1;
            right: 40px;
        }

        .fixed-table-body {
            overflow-x: visible !important;
            overflow-y: visible !important;
            height: 100%;
        }

        .tc {
            text-align: center;
        }
    </style>
</head>
<body class="gray-bg">
<div class="main-content">
    <form id="form-add" class="form-horizontal">
        <h4 class="form-header h4">表单信息</h4>
        <div class="row">
            <div class="col-sm-3">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">出库单号：</label>
                    <div class="col-sm-8">
                        <input name="orderId" class="form-control" th:value="${orderId}" type="text" required readonly>
                    </div>
                </div>
            </div>
            <div class="col-sm-3">
                <div class="form-group">
                    <label class="col-sm-4 control-label">领用类型：</label>
                    <div class="col-sm-8">
                        <select name="deliveryType" class="form-control m-b">
                            <option value="生产领用">生产领用</option>
                            <option value="检修领用">检修领用</option>
                            <option value="销售领用">销售领用</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-sm-3">
                <div class="form-group">
                    <label class="col-sm-4 control-label">领用单位：</label>
                    <div class="col-sm-8">
                        <input name="department" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-sm-3">
                <div class="form-group">
                    <label class="col-sm-4 control-label">领用工段：</label>
                    <div class="col-sm-8">
                        <input name="section" class="form-control" type="text">
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-3">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">出库时间：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <input name="outboundTime" class="form-control" placeholder="yyyy-MM-dd" type="text"
                                   required>
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-3">
                <div class="form-group">
                    <label class="col-sm-4 control-label">审批人：</label>
                    <div class="col-sm-8">
                        <input name="approver" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-sm-3">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">领料人：</label>
                    <div class="col-sm-8">
                        <input name="picker" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-sm-3">
                <div class="form-group">
                    <label class="col-sm-4 control-label">仓管员：</label>
                    <div class="col-sm-8">
                        <input name="warehouseKeeper" class="form-control" type="text">
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <label class="col-xs-1 control-label">备注：</label>
                    <div class="col-xs-11">
                        <textarea name="mark" maxlength="500" class="form-control" rows="3"></textarea>
                    </div>
                </div>
            </div>
        </div>
        <h4 class="form-header h4">物料信息</h4>
        <div class="row">
            <div class="col-sm-12">
                <button type="button" class="btn btn-white btn-sm" onclick="addRow()"><i class="fa fa-plus"> 增加</i>
                </button>
                <button type="button" class="btn btn-white btn-sm" onclick="sub.delRow()"><i class="fa fa-minus">
                    删除</i></button>

                <label>供应商：</label><select id="supplier-select" class="supplier-select"></select>
                <div class="col-sm-12 select-table table-striped">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </form>
</div>

<div class="row">
    <div class="col-sm-offset-5 col-sm-10">
        <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存
        </button>&nbsp;
        <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭
        </button>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: datetimepicker-js"/>
<th:block th:include="include :: bootstrap-suggest-js"/>
<script th:src="@{/js/jquery.tmpl.js}"></script>
<th:block th:include="include :: select2-css"/>
<th:block th:include="include :: select2-js"/>
<script th:inline="javascript">
    $('.supplier-select').select2({
        placeholder: '--请选择供应商--',
        width: '250px',
        ajax: {
            url: '/warehouse/supplier/selectList',
            delay: 250,
            data: function (params) {
                return {
                    supplierName: params.term
                };
            }
        },
        minimumInputLength: 2

    });

    $(function () {
        var options = {
            pagination: false,
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            sidePagination: "client",
            columns: [{
                checkbox: true
            }, {
                field: 'index',
                align: 'center',
                title: "序号",
                formatter: function (value, row, index) {
                    var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                    var columnId = $.common.sprintf("<input type='hidden' name='goods[%s].id' value='%s'>", index, row.id);
                    return columnIndex + $.table.serialNumber(index) + columnId;
                }
            }, {
                field: 'materialId',
                align: 'center',
                title: '物料编号',
                formatter: function (value, row, index) {
                    return $.common.sprintf(
                        "<div class='col-sm-11' style='display: inline-flex'>" +
                        "<input name='goods[%s].materialId' class='form-control materialId' autocomplete='off' type='text' value='%s'>" +
                        "<div class='input-group-btn'>" +
                        "   <button type='button' class='btn btn-white dropdown-toggle' style='height: 31px' data-toggle='dropdown'>" +
                        "      <span class='caret'></span>" +
                        "   </button>" +
                        "   <ul class='dropdown-menu dropdown-menu-right' role='menu'>" +
                        "   </ul>" +
                        "</div></div>", index, value, index);
                }
            }, {
                field: 'materialName',
                align: 'center',
                title: '物料名称',
                formatter: function (value, row, index) {
                    return $.common.sprintf("<input class='form-control tc' type='text' name='goods[%s].materialName' value='%s' readonly>", index, value);
                }
            }, {
                field: 'requestNum',
                align: 'center',
                title: '申领数量',
                formatter: function (value, row, index) {
                    return $.common.sprintf("<input class='form-control tc' type='text' name='goods[%s].requestNum' value='%s'>", index, value);
                }
            }, {
                field: 'actualOutboundNum',
                align: 'center',
                title: '实发数量',
                formatter: function (value, row, index) {
                    return $.common.sprintf("<input class='form-control tc actualOutboundNum' type='text' name='goods[%s].actualOutboundNum' value='%s' required>", index, value);
                }
            }, {
                field: 'inventoryQuantity',
                align: 'center',
                title: '库存数量',
                formatter: function (value, row, index) {
                    return $.common.sprintf("<input class='form-control tc' type='text' name='goods[%s].inventoryQuantity' value='%s' readonly>", index, value);
                }
            }, {
                field: 'unit',
                align: 'center',
                title: '单位',
                formatter: function (value, row, index) {
                    return $.common.sprintf("<input class='form-control tc' type='text' name='goods[%s].unit' value='%s' readonly>", index, value);
                }
            }, {
                field: 'mark',
                align: 'center',
                title: '备注',
                formatter: function (value, row, index) {
                    return $.common.sprintf("<input class='form-control tc' type='text' name='goods[%s].mark' value='%s'>", index, value);
                }
            }, {
                field: 'supplierId',
                align: 'center',
                title: '供应商Id',
                visible: false,
                formatter: function (value, row, index) {
                    return $.common.sprintf("<input class='form-control tc' type='text' name='goods[%s].supplierId' value='%s' readonly>", index, value);
                }
            }, {
                field: 'supplierName',
                align: 'center',
                title: '供应商',
                formatter: function (value, row, index) {
                    return $.common.sprintf("<input class='form-control tc' type='text' name='goods[%s].supplierName' value='%s' readonly>", index, value);
                }
            }, {
                title: '操作',
                align: 'center',
                formatter: function (value, row, index) {
                    var value = $.common.isNotEmpty(row.index) ? row.index : $.table.serialNumber(index);
                    return '<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="sub.delRowByIndex(\'' + value + '\')"><i class="fa fa-remove"></i>删除</a>';
                }
            }]
        };
        $.table.init(options);
    });

    /* 主子表-提交 */
    function submitHandler(index, layero) {
        if ($.validate.form()) {
            var data = $("#form-add").serializeArray();
            $.operate.saveTab("/warehouse/deliveryOrder/add", data);
        }
    }

    $("input[name='outboundTime']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    function addRow() {
        var count = $("#" + table.options.id).bootstrapTable('getData').length;
        var res = $("#supplier-select").select2("data");
        var supplierId = '-1';
        var supplierName = "";
        if (res.length > 0) {
            supplierId = res[0].id;
            supplierName = res[0].text;
        }
        var row = {
            index: $.table.serialNumber(count),
            materialId: "",
            requestNum: "",
            actualOutboundNum: "",
            materialName: "",
            inventoryQuantity: "",
            unit: "",
            mark: "",
            supplierId: supplierId,
            supplierName: supplierName
        }
        sub.addRow(row);
    }

    $("#bootstrap-table").on("post-body.bs.table", function (e, args) {
        $('.actualOutboundNum').on('blur', function (e) {
            let i = e.currentTarget.name.split('.')[0];
            let inventoryQuantity = $("input[name='" + i + ".inventoryQuantity']").val();
            let actualOutboundNum = $(this).val();
            if (Number(actualOutboundNum) > Number(inventoryQuantity)) {
                $.modal.alertError("实发数量不能大于库存数量");
                $(this).val("");
            }
        })

        var res = $("#supplier-select").select2("data");
        var supplierId = "-1";
        if (res.length > 0) {
            supplierId = res[0].id;
        }
        $('.materialId').bsSuggest({
            idField: "materialId",      // 每组数据的哪个字段作为 data-id，优先级高于 indexId 设置（推荐）
            keyField: "materialId",     // 每组数据的哪个字段作为输入框内容，优先级高于 indexKey 设置（推荐）
            allowNoKeyword: false,      // 是否允许无关键字时请求数据
            hideOnSelect: true,         // 鼠标从列表单击选择了值时，是否隐藏选择列表
            multiWord: true,            // 以分隔符号分割的多关键字支持
            separator: ",",             // 多关键字支持时的分隔符，默认为空格
            getDataMethod: "url",       // 获取数据的方式，总是从 URL 获取
            effectiveFields: ["materialId", "materialName", "inventoryQuantity", "unit"],
            effectiveFieldsAlias: {
                'materialId': "物料编码",
                'materialName': "物料名称",
                'inventoryQuantity': '库存数量',
                'unit': '单位'
            },
            showHeader: true,
            url: '/warehouse/materialInfo/getMaterials?supplierId=' + supplierId + '&materialId='
        }).on("onSetSelectValue", function (e, selectedData, selectedRawData) {
            let i = e.currentTarget.name.split('.')[0];
            $("input[name='" + i + ".inventoryQuantity']").val(selectedRawData.inventoryQuantity);
            $("input[name='" + i + ".unit']").val(selectedRawData.unit);
            $("input[name='" + i + ".materialName']").val(selectedRawData.materialName);
        });
    });
</script>
</body>
</html>